package com.meilingcloud.biobank.controller;

import com.meilingcloud.biobank.domain.FamilySource;
import com.meilingcloud.biobank.service.IFamilySourceService;
import com.meilingcloud.common.annotation.Log;
import com.meilingcloud.common.core.controller.BaseController;
import com.meilingcloud.common.core.domain.AjaxResult;
import com.meilingcloud.common.enums.BusinessType;
import com.meilingcloud.common.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 家系样本源 Controller
 *
 * @author chenl
 * @date 2021-06-22
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/biobank/familySource")
public class FamilySourceController extends BaseController {

    private final IFamilySourceService iFamilySourceService;

    /**
     * 查询家系样本源 列表
     */
    @PreAuthorize("@ss.hasPermi('biobank:familySource:list')")
    @GetMapping("/list")
    public AjaxResult list() {
        List<FamilySource> list = iFamilySourceService.list();
        return AjaxResult.success(list);
    }

    /**
     * 获取家系样本源 详细信息
     */
    @PreAuthorize("@ss.hasPermi('biobank:familySource:query')")
    @GetMapping(value = "/{familySourceId}")
    public AjaxResult getInfo(@PathVariable("familySourceId") Long familySourceId) {
        return AjaxResult.success(iFamilySourceService.getInfoById(familySourceId));
    }

    /**
     * 新增家系样本源
     */
    @PreAuthorize("@ss.hasPermi('biobank:familySource:add')")
    @Log(title = "家系图谱 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody FamilySource familySource) {
        familySource.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iFamilySourceService.saveSource(familySource));
    }

    /**
     * 新增家系关联样本源
     *
     * @return
     */
    @PreAuthorize("@ss.hasPermi('biobank:familySource:add')")
    @Log(title = "家系图谱 ", businessType = BusinessType.UPDATE)
    @PostMapping("/addRelate")
    public AjaxResult addRelate(@Valid @RequestBody FamilySource familySource) {
        familySource.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iFamilySourceService.saveRelateSource(familySource));
    }

    @PreAuthorize("@ss.hasPermi('biobank:familySource:remove')")
    @Log(title = "家系图谱 ", businessType = BusinessType.UPDATE)
    @PostMapping("/delRelate")
    public AjaxResult delRelate(@RequestBody FamilySource familySource) {
        return toAjax(iFamilySourceService.delRelateSource(familySource));
    }

    /**
     * 修改家系样本源
     */
    @PreAuthorize("@ss.hasPermi('biobank:familySource:edit')")
    @Log(title = "家系图谱 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody FamilySource familySource) {
        familySource.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(iFamilySourceService.updateById(familySource));
    }

    /**
     * 删除家系样本源
     */
    @PreAuthorize("@ss.hasPermi('biobank:familySource:remove')")
    @Log(title = "家系图谱 ", businessType = BusinessType.DELETE)
    @DeleteMapping("/{familySourceId}")
    public AjaxResult remove(@PathVariable("familySourceId") Long familySourceId) {
        return toAjax(iFamilySourceService.removeSourceById(familySourceId));
    }
}
